const {ccclass, property} = cc._decorator;

@ccclass
export default class TimeModule {

    public static _instance: TimeModule = null;

    private dates: string[] = [];

    private dateMap: Object = {};

    public static getInstance(): TimeModule {
        if (TimeModule._instance == null) {
            TimeModule._instance = new TimeModule();
        }
        return TimeModule._instance;
    }

    initDates (dates: string[]) {
        this.dates = dates;
        for (let index = 0; index < dates.length; index++) {
            const date = dates[index];
            this.dateMap[date] = index;
        }
    }

    strToday () {
        var date = new Date();
        var strDate = '' + date.getFullYear();
        if (date.getMonth() < 9) {
            strDate += '0';
        }
        strDate += (date.getMonth() + 1);
        if (date.getDate() < 10) {
            strDate += '0';
        }
        strDate += date.getDate();
        return strDate;
    }

    //获取当前日期前几天
    strPreDate (addDay) {   
        var date = new Date();  
        date.setDate(date.getDate() - addDay); //获取AddDayCount天后的日期  
        var dt =date.getDay();
        if (dt == 0 || dt == 6) {
            return '';
        }
        var strDate = '' + date.getFullYear();
        if (date.getMonth() < 9) {
            strDate += '0';
        }
        strDate += (date.getMonth() + 1);
        if (date.getDate() < 10) {
            strDate += '0';
        }
        strDate += date.getDate();
        return strDate;
    }

    //获取当前日期前几天
    preDate (startDate: string, preCount: number) {
        let year = startDate.slice(0,4);
        let month = startDate.slice(4,6);
        let day = startDate.slice(6,8); 
        var date = new Date(year + '-' + month + '-' + day);  
        date.setDate(date.getDate() - preCount); //获取AddDayCount天后的日期  
        var dt =date.getDay();
        if (dt == 0 || dt == 6) {
            return '';
        }
        var strDate = '' + date.getFullYear();
        if (date.getMonth() < 9) {
            strDate += '0';
        }
        strDate += (date.getMonth() + 1);
        if (date.getDate() < 10) {
            strDate += '0';
        }
        strDate += date.getDate();
        return strDate;
    }

    /**
    * 格式化当前时间 精确到毫秒
    */
    formatTimeMs (timeMs = null) {
        var d = new Date();
        if (timeMs) {
            d = new Date(timeMs);    //根据时间戳生成的时间对象
        }
        var timeStr = '';
        if (d.getHours() < 10) {
            timeStr += '0';
        }
        timeStr += d.getHours() + ":";
        if (d.getMinutes() < 10) {
            timeStr += '0';
        }
        timeStr += d.getMinutes() + ":";
        if (d.getSeconds() < 10) {
            timeStr += '0';
        }
        timeStr += d.getSeconds();
        timeStr += '.';
        timeStr += d.getMilliseconds();
        return timeStr;
    }

    //获取最近交易日
    getRecentTradeDates (count) {
        let dates = this.dates.slice(0, count);
        return dates;
    }

    curTradeDate () {
        if (this.dates.length > 0) {
            return this.dates[0];
        }
        return this.strToday();
    }

    nextTradeDate (date) {
        for (let index = 0; index < this.dates.length; index++) {
            const item = this.dates[index];
            if (date == item && index > 0) {
                return this.dates[index - 1];
            }
        }
        return null;
    }

    preTradeDate () {
        if (this.dates.length > 0) {
            return this.dates[1];
        }
    }

    getDate (strDate) {
        let year = strDate.slice(0,4);
        let month = strDate.slice(4,6);
        let day = strDate.slice(6,8); 
        var date = new Date(year + '-' + month + '-' + day);
        return date;
    }

    //获取开始日期到结束日期第几周
    getWeekCount (startDate, endDate) {
        let d1 = this.getDate(startDate);
        let d2 = this.getDate(endDate);
        d1 = this.getWeekStartByDate(d1);
        let invCount = Math.ceil((d2.valueOf() - d1.valueOf()) / (24 * 60 * 60 * 1000)) + 1;
        let num = Math.ceil(invCount / 7);
        return num;
    }

    getDateFromDay(dayDate, day) {
        let date = new Date();
        date.setTime(dayDate.getTime() + day * 24 * 60 * 60 * 1000);
        return date;
    }

    getWeekStartByDate(date) {
        let day = date.getDay();
        return this.getDateFromDay(date, -day);
    }

    calcTradeCount (start, end) {
        let count = 0;
        count =  this.dateMap[start] - this.dateMap[end] + 1; 
        return count;
    }
}
